import 'package:flutter/material.dart';
import '../services/Storage.dart';
import '../widget/jdText.dart';
import '../widget/jdButton.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../config/config.dart';
import 'dart:convert';
import '../pages/tabs/tab.dart';
class Register3Page extends StatefulWidget {
  final Map arguments;
  Register3Page({Key key,this.arguments}) : super(key: key);

  _Register3PageState createState() => _Register3PageState();
}

class _Register3PageState extends State<Register3Page> {
  String tel;
  String code;
  String password;
  String rpassword;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.tel = widget.arguments['tel'];
    this.code = widget.arguments['code'];
  }

  doRegister() async{
      if(this.password.length < 6){
         Fluttertoast.showToast(
          msg: "密码不能少于6位数",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );  
        return;
      }
      if(this.password != this.rpassword){
         Fluttertoast.showToast(
          msg: "输入两次密码不一致",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );  
        return;
      } 
      var api = "${Config.domain}api/register";
      var reslut = await Dio().post(api,data: {
        "tel":this.tel,
        "code":this.code,
        "password":this.password
      });
      print({
        "tel":this.tel,
        "code":this.code,
        "password":this.password
      });
      print(reslut);
      if(reslut.data['success']){  
        Fluttertoast.showToast(
          msg: "${reslut.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
        Storage.setString('userinfo',json.encode(reslut.data['userinfo']));

        // 这个相当于跳回跟页面  new Tabs 就是页面  记住还要把 import '../tabs.dart'; 页面引进来
        Navigator.of(context).pushAndRemoveUntil(
          new MaterialPageRoute(builder: (context) => new Tabs()),
          (route) => route == null
        );
      }else{
        Fluttertoast.showToast(
          msg: "${reslut.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
      }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("注册 - 3"),
      ),
      body: ListView(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        children: <Widget>[ 
          SizedBox(height: 20,),
          JdText(
            password: true,
            text: '请输入密码',
            onChanged: (value){
              setState(() {
                this.password = value; 
              });
            },
          ),
           SizedBox(height: 20,),
          JdText( 
            password: true,
            text: '请输入确认密码',
            onChanged: (value){
              setState(() {
                this.rpassword = value; 
              });
            },
          ),
          SizedBox(height: 20,),
          JdButtoon(
            color: Colors.red,
            text: '确定',
            onTap: this.doRegister,
          )
        ],
      ),
    );
  }
}